package service

import (
	"duoke/model"
	"duoke/tables"
	utils "duoke/utils/common"
	"duoke/utils/orm"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/v2/text/gstr"
)

var CourseList = new(courseListService)

type courseListService struct {
	TablesName string
}

func (s *courseListService) Init() *courseListService {
	s.TablesName = "course_list"
	return s
}

func (s *courseListService) Save(c *gin.Context, req *model.CourseInfo) (int64, error) {
	accountR, _ := Request.Account(c)
	where := ""
	if req.Id > 0 {
		where = fmt.Sprintf("acid=%d and id=%d", accountR.Id, req.Id)
	}
	req.Acid = accountR.Id
	var data int64
	var err error
	if req.Id > 0 {
		data, err = orm.Db.Where(where).Omit("number_of_learners").AllCols().Update(req)
	} else {
		data, err = tableCommon.SaveData(s.TablesName, req, where, "insert")
	}
	if err != nil {
		return 0, err
	}
	return data, err
}
func (s *courseListService) Del(id int) (int64, error) {
	i, err := orm.Db.ID(id).Delete(&tables.CourseList{})
	if err != nil {
		return 0, err
	}
	return i, nil
}

// List 列表
func (s *courseListService) List(c *gin.Context, req *model.ReqList) (*[]tables.CourseList, int64, error) {
	// 查询结果
	where := ""
	if req.Keyword != "" {
		where = fmt.Sprintf("title like '%s'", "%"+req.Keyword+"%")
	}
	if req.GroupId != "" {
		where += fmt.Sprintf("group_id=%d", req.GroupId)
	}
	var list []tables.CourseList
	total, err := tableCommon.QueryListWithPageV2(where, req.PageNo,
		req.PageSize).FindAndCount(&list)
	if err != nil {
		return nil, 0, err
	}
	return &list, total, nil
}
func (s *courseListService) MenuList(c *gin.Context, req *model.CourseListReq) (
	*[]tables.CourseList, int64, error) {
	// 查询结果
	where := "1 = 1"
	if req.Id != 0 {
		where += fmt.Sprintf(" and menu_id=%d", req.Id)
	}
	if req.Ids != nil && req.Id == 0 {
		toString := utils.Common.ArrayToString(req.Ids)
		where += fmt.Sprintf(" and menu_id in (%s)", toString)
	}
	if req.InstructorId != 0 {
		where += fmt.Sprintf(" and instructor_id=%d", req.InstructorId)
	}
	list := make([]tables.CourseList, 0)
	total, err := tableCommon.QueryListWithPageV2(where, req.PageNo,
		req.PageSize).FindAndCount(&list)
	if err != nil {
		return nil, 0, err
	}
	return &list, total, nil
}
func (s *courseListService) Info(c *gin.Context, value interface{}, field string) (
	*model.CourseInfo, error) {
	// 获取详情
	user := new(model.CourseInfo)
	has, err := orm.Db.Where("id=?", value).Cols(field).Get(user)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, errors.New("该课程不存在")
	}
	// 判断参数是否在白名单范围内
	info, err := User.Info(c, "phone")
	if err != nil {
		return nil, errors.New("用户获取失败")
	}
	if info.Phone != "" && gstr.InArray(gstr.Split(info.Phone, ","), user.WhiteList) {
		user.IsBuy = 1
	}
	// 判断是否已收藏
	m := new(tables.CourseLecturer)
	get, err := orm.Db.Where("id=?", user.InstructorId).Get(m)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("当前教师不存在")
	}
	user.LecturerName = m.Name
	user.LecturerCollege = m.College
	user.LecturerPic = m.Pic
	return user, nil
}
func (s *courseListService) Statistics(c *gin.Context, where string) (
	*model.StaticsCourse, error) {
	account, err := Request.Account(c)
	if err != nil {
		return nil, err
	}
	// 获取详情
	var m model.StaticsCourse
	db := orm.Db
	if where == "" {
		db.Where(where)
	}
	_, err = db.Where("acid=?", account.Id).
		Select("sub_mch_id,sum(collect) as collect,sum(shares) as shares,sum(buy) as buy," +
			"sum(number_of_learners) as number_of_learners").Get(&m)
	if err != nil {
		return nil, err
	}
	return &m, nil
}

func (s *courseListService) CourseVideo(c *gin.Context, id int) (*model.StaticsCourseVideo, error) {
	account, err := Request.Account(c)
	if err != nil {
		return nil, err
	}
	var m model.StaticsCourseVideo
	_, err = orm.Db.Where("acid=? and list_id=?", account.Id, id).Table("course_video").
		Select("sum(watch_number) as watch_number,sum(watch_time) as watch_time").Get(&m)
	return &m, err
}
